package com.ibl.rover.lite.client.Activity;

import com.ibl.rover.lite.client.Logger;
import com.ibl.rover.lite.client.R;
import com.ibl.rover.lite.client.Application.ApplicationContext;

import android.R.integer;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SettingActivity extends Activity implements OnClickListener {

	private Logger logger = Logger.getLogger();
	private ApplicationContext context;
	private Resources resources;
	private EditText hostAddress, hostPort;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);

		context = (ApplicationContext) getApplicationContext();
		((Button) this.findViewById(R.id.settingsave)).setOnClickListener(this);

		((Button) this.findViewById(R.id.setting_cancel_button))
				.setOnClickListener(this);
		hostAddress = ((EditText) this.findViewById(R.id.hostaddress));

		String serverUrl, serverPort;

		SharedPreferences app_preferences = getSharedPreferences("SETTING", MODE_WORLD_WRITEABLE);
		
		serverUrl = app_preferences.getString("IP", context.getAppParams()
				.getHttpConfiguration().getServerUrl());
		serverPort = app_preferences.getString(
				"PORT",
				String.valueOf(context.getAppParams().getHttpConfiguration()
						.getServerPort()));

		hostAddress.setText(serverUrl);
		hostPort = ((EditText) this.findViewById(R.id.hostport));
		hostPort.setText(serverPort);

		resources = getResources();
	}

	@Override
	public void onClick(View v) {
		try {
			switch (v.getId()) {
			case R.id.settingsave:
				saveSetting();
				break;
			case R.id.setting_cancel_button:
				this.finish();
				break;
			default:
				break;
			}
		} catch (Exception e) {
			logger.logException("onClick Exception ", e);
		}
	}

	/**
	 * Save Setting
	 */
	private void saveSetting() {
		try {

			if (!hostAddress.getText().toString().equals("")
					&& !hostPort.getText().toString().equals("")) {
				context.getAppParams()
						.getHttpConfiguration()
						.setServerPort(
								Integer.parseInt(hostPort.getText().toString()
										.trim()));
				context.getAppParams().getHttpConfiguration()
						.setServerUrl(hostAddress.getText().toString().trim());

				SharedPreferences app_preferences = getSharedPreferences("SETTING", MODE_WORLD_WRITEABLE);

				SharedPreferences.Editor editor = app_preferences.edit();
				editor.putString("IP", context.getAppParams()
						.getHttpConfiguration().getServerUrl());

				editor.putString(
						"PORT",
						String.valueOf(context.getAppParams()
								.getHttpConfiguration().getServerPort()));
				editor.commit();
				app_preferences.getString("IP","3");
				
				context.showMessage("Setting save Successfully",
						Toast.LENGTH_SHORT);

				this.finish();
			} else {
				context.showMessage(
						"Please Enter the Host Address and Host Port .",
						Toast.LENGTH_SHORT);
			}

		} catch (Exception e) {
			logger.logException("Save Setting ", e);
		}
	}
}
